package com.wutao.panzh.admin.content.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.panzh.UtilEntity.ResultVO;
import com.panzh.entity.index.UserInfo;
import com.panzh.myconst.CommonConst;
import com.panzh.myconst.ProjectParames;
import com.wutao.panzh.service.UserInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserInfoController {

    @Reference
    private UserInfoService userInfoService;


    //执行分页的方法：
    @ResponseBody
    @RequestMapping("/user/info/get/page")
    public ResultVO<PageInfo<UserInfo>> getUserPage(
            //由于pageNo在请求参数中不一定能够提供，所以设置默认值。
            //默认值为1表示未提供此数据时前往第1页
            @RequestParam(value="pageNo",defaultValue="1") Integer pageNo,
            //由于keyword在请求参数中不一定能够提供，所以设置默认值。
            //默认值为空字符串将不会对SQL语句有任何影响
            @RequestParam(value="keyword",defaultValue="") String keyword){

        PageInfo<UserInfo> informationItemPageInfo = userInfoService.getUserInfoPageInfo(pageNo, ProjectParames.PAGE_SIZE, keyword);

        return new ResultVO<PageInfo<UserInfo>>(ResultVO.SUCCESS,ResultVO.NO_MSG ,informationItemPageInfo);
    }

    @ResponseBody
    @RequestMapping("/reset/pass/word/userInfo")
    public ResultVO resetPassWordUserInfo(@RequestParam("userlId") String userlId){
        try {
            userInfoService.resetPassWordUserInfo(userlId);
        } catch (Exception e) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.RESET_PASSWORD_FAILED,ResultVO.NO_DATA);
        }
        return new ResultVO<String>(ResultVO.SUCCESS,CommonConst.INFORMATION_SAVED_SUCCESSFULLY ,ResultVO.NO_DATA);
    }

    @ResponseBody
    @RequestMapping("/delete/userInfo")
    public ResultVO deleteUserInfo(@RequestParam("userlId") String userlId){
        try {
            userInfoService.deleteUserInfo(userlId);
        } catch (Exception e) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.DELETE_INFORMATION_EXCEPTION,ResultVO.NO_DATA);
        }
        return new ResultVO<String>(ResultVO.SUCCESS,CommonConst.DELETE_INFORMATION_SUCCESS ,ResultVO.NO_DATA);
    }

//    /sava/user/info
    @RequestMapping("/sava/user/info")
    public String savaUserInfo(@RequestParam("nickName") String nickName,
                                 @RequestParam("password") String password,
                                 @RequestParam("tel") String tel,
                                 @RequestParam("sex") String sex){
        List<UserInfo> userInfos = userInfoService.selectAllByTel(tel);
        //进行一个判断
        if (userInfos.size() == 0){
            userInfoService.savaUserInfo(nickName,password,tel,sex);
        }
        return "show_user";
    }



}
